<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>原型对象</h1>
<hr>

<script>

    //1.类对象和对象
    //一个对象默认继承自Object,因此它的原型对象与Object的原型对象相同。
    /*
    function Person(name,age){
        this.name = name;
        this.age = age;
    }
    let p = new Person('zhangsan',20);
    console.log(Person.prototype===p.__proto__);
    console.log(Object.prototype===Person.prototype.__proto__);
     */

    //2.在原型对象上扩展新的属性或者方法
    /*
    function Animal(height, weight) {
        this.height = height;
        this.weight = weight;
    }

    let a = new Animal();
    console.log(a.__proto__ === Animal.prototype);

    //在类的prototype上扩展
    Animal.prototype.eat = function () {
        console.log('animal eating...');
    }
    //在对象的__proto__属性上扩展
    a.__proto__.bark = function () {
        console.log('animal barking...');
    }

    a.eat();
    a.bark();

    let b = new Animal();
    console.log('创建新对象后....');
    b.eat();
    b.bark();
   */


    //3.原型实现继承
    /*
    function Animal(height,weight){
        this.height = height;
        this.weight = weight;
    }
    Animal.prototype.eat=function(){
        console.log('animal eating...');
    }

    function Human(name){
        this.name = name;
    }

    Human.prototype = new Animal();
    Human.prototype.think=function(){
        console.log(this.name+" is thinking...");
    }

    let person = new Human('zhangsan');
    person.eat();
    person.think();
    */

    //4.在Object的prototype上扩展

    function Animal(height,weight){
        this.height = height;
        this.weight = weight;
    }
    Animal.prototype.eat=function(){
        console.log('animal eating...');
    }

    function Human(name){
        this.name = name;
    }

    Human.prototype = new Animal();

    Object.prototype.think=function(){
        console.log(this.name+" is thinking...");
    }

    let person = new Human('zhangsan');
    person.eat();
    person.think();

    let obj = {name:'lisi'};
    obj.think();


</script>
</body>
</html>